package indi.zhifa.study2025.oa.service.role.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.study2025.oa.dao.gen.service.IGenRoleGroupDbService;
import indi.zhifa.study2025.oa.dao.po.GenPositionEntity;
import indi.zhifa.study2025.oa.dao.po.GenRoleGroupEntity;
import indi.zhifa.study2025.oa.service.role.IRoleGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class RoleGroupServiceImpl implements IRoleGroupService {

    final IGenRoleGroupDbService mGenRoleGroupDbService;

    protected void checkExistByRoleGroupCode(String roleGroupCode) {
        LambdaQueryWrapper<GenRoleGroupEntity> queryWrapper = Wrappers.<GenRoleGroupEntity>lambdaQuery()
                .eq(GenRoleGroupEntity::getCode,roleGroupCode);
        boolean exist = mGenRoleGroupDbService.exists(queryWrapper);
        if(exist){
            throw new ServiceException("已存在名为"+roleGroupCode+"的角色组");
        }
    }

    @Override
    public GenRoleGroupEntity create(GenRoleGroupEntity pRoleGroup) {
        checkExistByRoleGroupCode(pRoleGroup.getCode());
        if(!mGenRoleGroupDbService.save(pRoleGroup)) {
            throw new ServiceException("存储GenRoleGroupEntity失败");
        }
        return pRoleGroup;
    }

    @Override
    public GenRoleGroupEntity edit(Long pId, Object pRoleGroupEditDto) {
        GenRoleGroupEntity roleGroupEntity = mGenRoleGroupDbService.updatePull(pId, pRoleGroupEditDto);
        return roleGroupEntity;
    }

    @Override
    public GenRoleGroupEntity checkById(Long id) {
        GenRoleGroupEntity roleGroupEntity = mGenRoleGroupDbService.check(id);
        return roleGroupEntity;
    }

    @Override
    public GenRoleGroupEntity delete(Long id) {
        GenRoleGroupEntity delData = mGenRoleGroupDbService.deleteById(id);
        return delData;
    }

    @Override
    public List<GenRoleGroupEntity> findAll() {
        return mGenRoleGroupDbService.list();
    }
}
